package com.industrial.premu.controller;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuBenefitEvaluation;
import com.industrial.premu.domain.vo.DifferentiatedPolicySimulationVo;
import com.industrial.premu.service.IDifferentiatedPolicySimulationService;
import com.industrial.system.api.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/differentiated_policy")
public class DifferentiatedPolicySimulationController extends BaseController {

    @Autowired
    private IDifferentiatedPolicySimulationService differentiatedPolicySimulationService;

    /**
     * <p>
     *     查询列表
     * </p>
     * @param params
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(DifferentiatedPolicySimulationVo params){
        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if(ArrayUtil.contains(sysRoles.toArray(),"x_01_011")){
            if(StringUtils.isBlank(params.getReleaseCity())){
                params.setReleaseCounty(sysUser.getDistrict());
            }else{
                params.setReleaseCounty(params.getReleaseCity());
                params.setReleaseCity(null);
            }
            params.setReleaseCity(null);
        }else if(ArrayUtil.contains(sysRoles.toArray(),"s_01_011")){
            if(StringUtils.isBlank(params.getReleaseCity())){
                params.setReleaseCity(sysUser.getCity());
            }else{
                if(StringUtils.endsWith(params.getReleaseCity(),"0000")){
                    params.setReleaseProvince(params.getReleaseCity());
                    params.setReleaseCity(null);
                }else if(StringUtils.endsWith(params.getReleaseCity(),"00")){
                    params.setReleaseCity(params.getReleaseCity());
                }else{
                    params.setReleaseCounty(params.getReleaseCity());
                    params.setReleaseCity(null);
                }
            }
        }else if(ArrayUtil.contains(sysRoles.toArray(),"p_01_011")){
            if(StringUtils.isBlank(params.getReleaseCity())){
                params.setReleaseProvince(sysUser.getProvince());
            }else{
                if(StringUtils.endsWith(params.getReleaseCity(),"0000")){
                    params.setReleaseProvince(params.getReleaseCity());
                    params.setReleaseCity(null);
                }else if(StringUtils.endsWith(params.getReleaseCity(),"00")){
                    params.setReleaseCity(params.getReleaseCity());
                }else{
                    params.setReleaseCounty(params.getReleaseCity());
                    params.setReleaseCity(null);
                }
            }
        }

        startPage();
        List<DifferentiatedPolicySimulationVo> result = differentiatedPolicySimulationService.list(params);
        return getDataTable(result);
    }


    @GetMapping("/queryInfo/{preMuId}")
    public AjaxResult queryInfo(@PathVariable("preMuId") String preMuId){
        PreMuBenefitEvaluation preMuBenefitEvaluation = differentiatedPolicySimulationService.queryInfo(preMuId);

        return AjaxResult.success(preMuBenefitEvaluation);
    }
}
